package com.briup.pts.web.controller;

import com.briup.pts.aop.Logger;
import com.briup.pts.config.response.Result;
import com.briup.pts.domain.bean.Animal;
import com.briup.pts.domain.vo.AnimalVO;
import com.briup.pts.service.AnimalService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wy
 * @description
 * @data 2024/11/28  9:19
 */
@Tag(name = "动物模块")
@Slf4j
@RequestMapping("/animal")
@RequiredArgsConstructor
@RestController
public class AnimalController {
    private final AnimalService service;

    @Operation(summary = "根据动物ID生成对应的二维码信息")
    @GetMapping("/QRcode")
    public Result<?> qrcode(
            @NotBlank(message = "动物id必须存在") String animalId) throws Exception {
        // 调用service层接口，生成二维码
        service.qrcode(animalId);
        return Result.success();
    }


    @Operation(summary = "根据动物编号查询动物基本信息以及对应批次和栏舍栏圈信息")
    @GetMapping("/findByAnimalId")
    public Result<?> findByAnimalIdd(
            @NotBlank(message = "动物id必须传输") String animalId) {
        AnimalVO animalVO = service.findByAnimalId(animalId);
        return Result.success(animalVO)
                .apply("获取成功");
    }

    @Operation(summary = "分页并根据条件查询动物基本信息以及对应批次和栏舍栏圈信息")
    @GetMapping("/query")
    public Result<?> pageQuery(@NotNull(message = "分页参数不能为空")
                               @Min(value = 1L, message = "当前页面不能小于1") Integer pageNum,
                               @Min(value = 1L, message = "页面大小不能小于1")
                               @NotNull(message = "分页参数不能为空") Integer pageSize,
                               String aHealthy,String aGender) {
        // 调用service层方法，时间分页多条件查询
        PageInfo<AnimalVO> pageInfo = service
                .pageQueryByCondition(pageNum, pageSize,aHealthy, aGender);

        return Result.success(pageInfo)
                .apply("获取成功");
    }

    //更新或新增接口
    @Operation(summary = "新增或修改动物接口")
    @Logger("新增或修改动物接口")
    @PostMapping("/saveOrUpdate")
    public Result<?> saveOrUpdate(@RequestBody Animal animal) {
        service.saveOrUpdate(animal);
        return Result.success();
    }

    // 单删
    @Operation(summary = "根据id删除动物")
    @Logger("根据id删除动物")
    @DeleteMapping("/deleteById/{aAnimalId}")
    public Result<?> deleteById(@PathVariable String aAnimalId) {
        service.deleteById(aAnimalId);
        return Result.success(null)
                .apply("删除成功");
    }


    // 批量删除，id数组是通过json格式字符串传输的数据，在请求体中获取
    @Operation(summary = "批量删除动物")
    @Logger("批量删除动物")
    @DeleteMapping("/deleteByIdAll")
    public Result<?> deleteByIdAll(
            @NotEmpty(message = "没有要删除的质检报告") @RequestBody List<String> ids) {
        log.info("需要删除的书籍为:{}", ids);
        // 批量删除数据
        service.deleteByIdAll(ids);
        return Result.success(null)
                .apply("批量删除成功");
    }
}
